package com.jiaoyou.baojian.activity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiaoyou.baojian.R;
import com.jiaoyou.baojian.entity.response.LoginResponse;
import com.jiaoyou.baojian.http.Api;
import com.jiaoyou.baojian.http.BaofangCallback;
import com.jiaoyou.baojian.http.response.BaseResponse;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import retrofit2.Call;

import static com.jiaoyou.baojian.constvalue.ConstValue.SHARE_SDK_SMS_APPKEY;
import static com.jiaoyou.baojian.constvalue.ConstValue.SHARE_SDK_SMS_APPSECRET;
import static com.jiaoyou.baojian.constvalue.ConstValue.SHARE_SDK_SMS_COUNTRY;

public class VerifyCodeLoginActivity extends BaseActivity {

    @BindView(R.id.mobile)
    EditText mobile;
    @BindView(R.id.yanzhengma)
    EditText yanzhengma;
    @BindView(R.id.send)
    Button send;
    @BindView(R.id.password)
    EditText password;
    @BindView(R.id.button)
    Button button;
    @BindView(R.id.send_tip)
    TextView send_tip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ButterKnife.bind(this);

        setTitle("验证码登录");
    }

    @Override
    public int getContentView() {
        return R.layout.activity_verify_code_login;
    }

    @OnClick({R.id.send, R.id.button})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.send: {
                String s1 = mobile.getEditableText().toString();
                if (TextUtils.isEmpty(s1)) {
                    toast("电话不能为空");
                    return;
                }
                if(s1.startsWith("17")){
                    toast("不支持虚拟号段");
                    return;
                }
                waiting("发送短信...");
                Api.getInstance().sendLoginSMS(s1, new BaofangCallback() {
                    @Override
                    public void onFailure(Call call, Throwable t) {
                        stopWaiting();
                        toast("短信发送失败：" + t.getLocalizedMessage());
                    }

                    @Override
                    public void onSuccess(Object o) {
                        stopWaiting();
                        toast("短信已发送到您手机上");
                    }
                });
                disableSend();
            }
            break;
            case R.id.button: {
                String s1 = mobile.getEditableText().toString();
                String s2 = yanzhengma.getEditableText().toString();
                String s3 = password.getEditableText().toString();
                if (TextUtils.isEmpty(s1) || TextUtils.isEmpty(s2) || TextUtils.isEmpty(s3)) {
                    toast("数据不能为空");
                    return;
                }
                if(s1.startsWith("17")){
                    toast("不支持虚拟号段");
                    return;
                }
                waiting("登录中...");
                Api.getInstance().vcodelogin(s1, s3, s2,
                        new BaofangCallback<LoginResponse>(){
                            @Override
                            public void onFailure(Call<BaseResponse<LoginResponse>> call, Throwable t) {
                                toast(t.getLocalizedMessage());
                                stopWaiting();
                            }

                            @Override
                            public void onSuccess(LoginResponse response) {
                                getACache().put(LoginResponse.TAG, new Gson().toJson(response));
                                stopWaiting();
                                toast("登录成功");
                                Bundle bundle = new Bundle();
                                bundle.putString("mobile", mobile.getEditableText().toString());
                                bundle.putString("password", password.getEditableText().toString());
                                startActivity(LoginActivity.class, bundle);
                                finish();
                            }
                        });
            }
            break;
        }
    }

    private CountDownTimer timer;
    private void disableSend() {
        send.setVisibility(View.GONE);
        send_tip.setVisibility(View.VISIBLE);
        timer = new CountDownTimer(59000, 1000){

            @Override
            public void onTick(long millisUntilFinished) {
                send_tip.setText(millisUntilFinished / 1000 + "秒后再次发送");
            }

            @Override
            public void onFinish() {
                send_tip.setVisibility(View.GONE);
                send.setVisibility(View.VISIBLE);
            }
        };
        timer.start();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if(timer != null)
            timer.cancel();
    }
}
